package edu.uoc.tdp.pac3.client;


import java.io.IOException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.swing.JOptionPane;

import edu.uoc.tdp.pac3.common.StockManagerInterface;
import edu.uoc.tdp.pac3.ui.i18n.LanguageUtils;

public class Client {
	private StockManagerInterface remoto;
	private final String URL = "localhost";
	private final int PORT = 1099;
	private final String JNDI_NAME = "StockManager";

	public static void main(String args[]) throws IOException {
		Client cl = new Client();
		//cl.realizarTest();
	}

	public Client() {
		try {
			System.out.println("Conectando al servidor desde cliente....");
			Registry registry = LocateRegistry.getRegistry(URL, PORT);
			remoto = (StockManagerInterface) registry.lookup(JNDI_NAME);
			System.out.println("Conectado!");
		} catch (NotBoundException e) {
			JOptionPane.showMessageDialog(null, 
					LanguageUtils.getMessage("client.error.message.remote.bound"),
					LanguageUtils.getMessage("client.error.title"),
					JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(null, 
					LanguageUtils.getMessage("client.error.message.remote"),
					LanguageUtils.getMessage("client.error.title"),
					JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, 
					LanguageUtils.getMessage("client.error.message.generic"),
					LanguageUtils.getMessage("client.error.title"),
					JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		}
	}

	public StockManagerInterface getRemoteObject(){
		return remoto;
	}
	
}